#include "StdAfx.h"


namespace DLNLib
{
	MonthRecord::MonthRecord( const CString& _resource,const CString& _file,const CString& _USN )
		:resource(_resource)
		,file(_file)
		,USN(_USN)
		,MaxDaySize(0)
		,day(NULL)
	{
		SetTime();
		Init();//allocate [] day
		Load();//load from .out file
		Dispatch();
	}
	MonthRecord::~MonthRecord(void)
	{
		Save();
		for(size_t i=0;i<(size_t)MaxDaySize;i++)
			if(day[i])
				delete day[i];
		if(day)
			delete [] day;
		if(NextMonthFirstDay)
			delete NextMonthFirstDay;
	}

	void MonthRecord::SetTime()
	{
		char drive[256];
		char dir[256];
		char fname[256];
		char ext[256];

		_splitpath(file,drive,dir,fname,ext);

		int j = (int)strlen(fname)-1;
		
		for(;j>=0;j--)
			if(fname[j] == '_')
					break;
		char* p = fname+j+1;

		int lYearMonth = strlen(p);
		int lMonth = 2;
		int lYear = lYearMonth - lMonth;
		
		//char szYear[3];

		// maybe 10X or 9X, length is 2 or 3
		char szYear[4];
		ZeroMemory(szYear,4);
		strncpy(szYear,p,lYear);
		p += lYear;

		char szMonth[3];
		ZeroMemory(szMonth,3);
		strncpy(szMonth,p,lMonth);

		time.SetDate(atoi(szYear)+1911,atoi(szMonth),1);
	}

	void MonthRecord::Init()
	{
		MaxDaySize = SKTLib::GetMaxDay(time.GetYear(),time.GetMonth());
		day = new DayRecord*[MaxDaySize];
		COleDateTime t;
		for(size_t i=0;i<(size_t)MaxDaySize;i++)
		{
			t.SetDate(time.GetYear(),time.GetMonth(),(int)i+1);
			day[i] = new DayRecord(t,USN);
		}
		COleDateTime n = SKTLib::GetNextMonth(time.GetYear(),time.GetMonth());
		t.SetDate(n.GetYear(),n.GetMonth(),1);
		NextMonthFirstDay = new DayRecord(t,USN);
	}

	void MonthRecord::Load()
	{
		if(!PathFileExists(file))return;
		
		
		FILE* fp = fopen(file,"r");
		ASSERT(fp);
		char line[255];
		while(!feof(fp))
		{
			ZeroMemory(line,255);
			fgets(line,255,fp);
		
			//parse the line to dayrecord
			DayRecord temp(COleDateTime::GetCurrentTime(),USN);
			Parse( temp,line);
			if(temp.time.GetYear() == time.GetYear() &&
			   temp.time.GetMonth()== time.GetMonth())
			{
				*day[temp.time.GetDay()-1] = temp;
			}
			if(temp.time.GetYear() == NextMonthFirstDay->time.GetYear() &&
			   temp.time.GetMonth()== NextMonthFirstDay->time.GetMonth()&&
			   temp.time.GetDay()  == NextMonthFirstDay->time.GetDay())
			{
				*NextMonthFirstDay = temp;
			}
		}
		fclose(fp);
	}

	void MonthRecord::Save()
	{
		
		FILE* fp = fopen(file,"w");
		for(size_t i=0;i<(size_t)MaxDaySize;i++)
		{
			fprintf(fp,"%s\n",day[i]->GetDayRecord());
		}
		fprintf(fp,"%s\n",NextMonthFirstDay->GetDayRecord());
		fclose(fp);
		
	}

	void MonthRecord::Parse( DayRecord& rDay,char* rString )
	{
		ASSERT(rString);
		if(strlen(rString)==0)return;
		char* p = rString;

		p += 8;

		char szYear[3];
		ZeroMemory(szYear,3);
		strncpy(szYear,p,2);
		p += 2;

		char  szMonth[3];
		ZeroMemory(szMonth,3);
		strncpy(szMonth,p,2);
		p += 2;

		char szDay[3];
		ZeroMemory(szDay,3);
		strncpy(szDay,p,2);
		p += 2;

		rDay.time.SetDate(atoi(szYear)+1911,atoi(szMonth),atoi(szDay));


		char szValue[7];
		for(int i=0;i<24;i++)
		{
			ZeroMemory(szValue,7);
			strncpy(szValue,p,6);
			p += 6;

			if(strlen(szValue)==0)
				continue;
			rDay.hour[i].value = (float)atof(szValue);
		}
	}

	void MonthRecord::Dispatch()
	{
		ASSERT(PathFileExists(resource));
		FILE* fp = fopen(resource,"r");
		char line[255];
		
		Data temp;
		int i=0;
		int j=0;
		while(!feof(fp))
		{
			ZeroMemory(line,255);
			fgets(line,255,fp);
			
			if(Parse2(temp,line))
			{
				if(Append(temp))
					i++;
				j++;
			}
			
			
		}
	}

	bool MonthRecord::Append( const Data& rhs )
	{
		if(rhs.time.GetYear() == time.GetYear() &&
		   rhs.time.GetMonth()== time.GetMonth())
		{
			return day[rhs.time.GetDay()-1]->Append(rhs);
			
		}
		if(rhs.time.GetYear() == NextMonthFirstDay->time.GetYear() &&
		   rhs.time.GetMonth()== NextMonthFirstDay->time.GetMonth()&&
		   rhs.time.GetDay()  == NextMonthFirstDay->time.GetDay())
		{
			return NextMonthFirstDay->Append(rhs);
			
		}
		return false;
	}
	
}